<?php

class User_Model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $row
     * @return User
     */
    public static function getUserByRow($row)
    {
        $thatUser = new User;
        $thatUser->setId($row->id);
        $thatUser->setEmail($row->email);
        $thatUser->setNickname($row->nickname);
        $thatUser->setPassword($row->password);
        $thatUser->setSalt($row->salt);
        $thatUser->setPower($row->power);
        $thatUser->setRegIp($row->reg_ip);
        $thatUser->setRegTime($row->reg_time);
        $thatUser->setLoginIp($row->login_ip);
        $thatUser->setLoginTime($row->login_time);
        $thatUser->setActIp($row->act_ip);
        $thatUser->setActTime($row->act_time);
        $thatUser->setUserInfoId($row->user_info_id);
        $thatUser->setStudentInfoId($row->student_info_id);
        return $thatUser;
    }

    /**
     * @param User $thatUser
     * @return array
     */
    public static function getModelByUser($thatUser)
    {
        return array(
            'email'           => $thatUser->getEmail(),
            'nickname'        => $thatUser->getNickname(),
            'password'        => $thatUser->getPassword(),
            'salt'            => $thatUser->getSalt(),
            'power'           => $thatUser->getPower(),
            'reg_ip'          => $thatUser->getRegIp(),
            'reg_time'        => $thatUser->getRegTime(),
            'login_ip'        => $thatUser->getLoginIp(),
            'login_time'      => $thatUser->getLoginTime(),
            'act_ip'          => $thatUser->getActIp(),
            'act_time'        => $thatUser->getActTime(),
            'user_info_id'    => $thatUser->getUserInfoId(),
            'student_info_id' => $thatUser->getStudentInfoId()
        );
    }

    /**
     * @param int $id
     * @throws FormatException
     * @return User
     */
    public static function getUserById($id)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('user.*');
        $CI->db->from('user');
        $CI->db->where('user.id', $id);

        $query = $CI->db->get();
        $row   = $query->row();
        if ($row)
            return self::getUserByRow($row);
        else
            return null;

    }

    /**
     * @param string $email
     * @return User
     */
    public static function getUserByEmail($email)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('user.*');
        $CI->db->from('user');
        $CI->db->where('user.email', $email);

        $query = $CI->db->get();
        $row   = $query->row();
        if ($row)
            return self::getUserByRow($row);
        else
            return null;
    }

    /**
     * @param bool|int $limit
     * @param bool|int $offset
     * @return array
     */
    public static function getUserArray($limit = false, $offset = false)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('user.*');
        $CI->db->from('user');

        if ($limit !== false && is_numeric($limit)) {
            if ($offset === false || !is_numeric($offset))
                $offset = 0;
            $CI->db->limit($limit, $offset);
        }

        $query  = $CI->db->get();
        $result = $query->result();

        $array = array();
        foreach ($result as $row) {
            $array[] = self::getUserByRow($row);
        }
        return $array;
    }

    /**
     * @param User $thatUser
     * @throws DatabaseException
     * @return User
     */
    public static function add($thatUser)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $result = $CI->db->insert('user', self::getModelByUser($thatUser));
        if ($result)
            return self::getUserByID($CI->db->insert_id());
        else
            throw new DatabaseException('插入数据失败');
    }

    /**
     * @param User $thatUser
     * @throws DatabaseException
     * @return User
     */
    public static function update($thatUser)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->where('id', $thatUser->getID());
        $result = $CI->db->update('user', self::getModelByUser($thatUser));
        if ($result)
            return self::getUserByID($thatUser->getID());
        else
            throw new DatabaseException('更新数据失败');
    }

    /**
     * @param User $thatUser
     * @throws DatabaseException
     * @return bool
     */
    public static function delete($thatUser)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->where('id', $thatUser->getID());
        $result = $CI->db->delete('user');
        if ($result)
            return true;
        else
            throw new DatabaseException('删除数据失败');
    }

}
